package com.daxia.ogmall.web.controller.m;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daxia.ogmall.dto.DesignProductDTO;
import com.daxia.ogmall.dto.ProductDTO;
import com.daxia.ogmall.dto.api.ProductAPIDTO;
import com.daxia.ogmall.dto.api.info.ProductInfoAPIDTO;
import com.daxia.ogmall.model.Design;
import com.daxia.ogmall.model.Product;
import com.daxia.ogmall.support.MPage;

@Controller
@RequestMapping(value = "/m/product", produces = "application/json;charset=UTF-8")
public class MProductController extends MBaseController {
    
    @ResponseBody
    @RequestMapping("list")
    public String list(ProductDTO dto, String designId,  MPage page) throws Exception {

		List<ProductDTO> list = new ArrayList<ProductDTO>();

		if (StringUtils.isNotBlank(designId)) {
			DesignProductDTO query = new DesignProductDTO();
			Design design = new Design();
			design.setId(Long.parseLong(designId));
			query.setDesign(design);
			List<DesignProductDTO> dpList = designProductService.find(query, page);

			for (DesignProductDTO designProductDTO : dpList) {
				Product product = designProductDTO.getProduct();
				if (product != null) {
					ProductDTO productDTO = new ProductDTO();
					BeanUtils.copyProperties(product, productDTO );
					list.add(productDTO);
				}
			}
		} else {
			list = productService.find(dto, page);
		}

		return toApiOutput(list, ProductAPIDTO.class, ProductInfoAPIDTO.class, page);

	}
    
  
    
}
 